home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gs16spl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.3 KB  |  221 lines

  1. /* Copyright (C) 1995, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gs16spl.c,v 1.2 2000/09/19 19:00:25 lpd Exp $ */
  20. /* 16-bit access to print spooler from Win32s */
  21. /* by Russell Lang */
  22. /* 1995-11-23 */
  23.  
  24. /* 
  25.  * Ghostscript produces printer specific output
  26.  * which must be given to the print spooler.
  27.  * Under Win16, the APIs OpenJob, WriteSpool etc. are used
  28.  * Under Win32 and Windows 95/NT, the APIs OpenPrinter, WritePrinter etc.  
  29.  * are used.
  30.  * Under Win32s, the 16-bit spooler APIs are not available, and the
  31.  * 32-bit spooler APIs are not implemented.
  32.  * This purpose of this application is to provide a means for the Win32s
  33.  * version of Ghostscript to send output to the 16-bit spooler.
  34.  */
  35.  
  36. /*
  37.  * Usage:  gs16spl port filename
  38.  *
  39.  * filename will be sent to the spooler port.
  40.  */
  41.  
  42.  
  43. #define STRICT
  44. #include <windows.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48.  
  49. #define ID_TEXT 100
  50.  
  51. /* documented in Device Driver Adaptation Guide */
  52. /* Prototypes taken from print.h */
  53. DECLARE_HANDLE(HPJOB);
  54.  
  55. HPJOB WINAPI OpenJob(LPSTR, LPSTR, HPJOB);
  56. int WINAPI StartSpoolPage(HPJOB);
  57. int WINAPI EndSpoolPage(HPJOB);
  58. int WINAPI WriteSpool(HPJOB, LPSTR, int);
  59. int WINAPI CloseJob(HPJOB);
  60. int WINAPI DeleteJob(HPJOB, int);
  61. int WINAPI WriteDialog(HPJOB, LPSTR, int);
  62. int WINAPI DeleteSpoolPage(HPJOB);
  63.  
  64. #define MAXSTR 256
  65. #define PRINT_BUF_SIZE 16384
  66.  
  67. HPJOB hJob;
  68. HWND hwndspl;
  69. DLGPROC lpfnSpoolProc;
  70. HINSTANCE phInstance;
  71. char port[MAXSTR];
  72. char filename[MAXSTR];
  73. char error_message[MAXSTR];
  74. int error;
  75.  
  76. char szAppName[] = "GS Win32s/Win16 spooler";
  77.  
  78. /* returns TRUE on success, FALSE on failure */
  79. int
  80. spoolfile(char *portname, char *filename)
  81. {
  82.     FILE *f;
  83.     char *buffer;
  84.     char pcdone[64];
  85.     long ldone;
  86.     long lsize;
  87.     int count;
  88.     MSG msg;
  89.  
  90.     if ((*portname == '\0') || (*filename == '\0')) {
  91.     strcpy(error_message, "Usage: gs16spl port filename");
  92.     return FALSE;
  93.     }
  94.     if ((buffer = malloc(PRINT_BUF_SIZE)) == (char *)NULL)
  95.     return FALSE;
  96.  
  97.     if ((f = fopen(filename, "rb")) == (FILE *) NULL) {
  98.     sprintf(error_message, "Can't open %s", filename);
  99.     free(buffer);
  100.     return FALSE;
  101.     }
  102.     fseek(f, 0L, SEEK_END);
  103.     lsize = ftell(f);
  104.     if (lsize <= 0)
  105.     lsize = 1;
  106.     fseek(f, 0L, SEEK_SET);
  107.     ldone = 0;
  108.  
  109.     hJob = OpenJob(portname, filename, (HDC) NULL);
  110.     switch ((int)hJob) {
  111.     case SP_APPABORT:
  112.     case SP_ERROR:
  113.     case SP_OUTOFDISK:
  114.     case SP_OUTOFMEMORY:
  115.     case SP_USERABORT:
  116.         fclose(f);
  117.         free(buffer);
  118.         return FALSE;
  119.     }
  120.     if (StartSpoolPage(hJob) < 0)
  121.     error = TRUE;
  122.  
  123.     while (!error
  124.        && (count = fread(buffer, 1, PRINT_BUF_SIZE, f)) != 0) {
  125.     if (WriteSpool(hJob, buffer, count) < 0)
  126.         error = TRUE;
  127.     ldone += count;
  128.     sprintf(pcdone, "%d%% written to %s", (int)(ldone * 100 / lsize), portname);
  129.     SetWindowText(GetDlgItem(hwndspl, ID_TEXT), pcdone);
  130.     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
  131.         TranslateMessage(&msg);
  132.         DispatchMessage(&msg);
  133.     }
  134.     }
  135.     free(buffer);
  136.     fclose(f);
  137.  
  138.     EndSpoolPage(hJob);
  139.     if (error)
  140.     DeleteJob(hJob, 0);
  141.     else
  142.     CloseJob(hJob);
  143.     return !error;
  144. }
  145.  
  146.  
  147. /* Modeless dialog box - main window */
  148. BOOL CALLBACK _export
  149. SpoolDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  150. {
  151.     switch (message) {
  152.     case WM_INITDIALOG:
  153.         SetWindowText(hDlg, szAppName);
  154.         return TRUE;
  155.     case WM_COMMAND:
  156.         switch (LOWORD(wParam)) {
  157.         case IDCANCEL:
  158.             error = TRUE;
  159.             DestroyWindow(hDlg);
  160.             EndDialog(hDlg, 0);
  161.             PostQuitMessage(0);
  162.             return TRUE;
  163.         }
  164.     }
  165.     return FALSE;
  166. }
  167.  
  168.  
  169. void
  170. init_window(LPSTR cmdline)
  171. {
  172.     LPSTR s;
  173.     char *d;
  174.  
  175.     s = cmdline;
  176.     /* skip leading spaces */
  177.     while (*s && *s == ' ')
  178.     s++;
  179.     /* copy port name */
  180.     d = port;
  181.     while (*s && *s != ' ')
  182.     *d++ = *s++;
  183.     *d = '\0';
  184.     /* skip spaces */
  185.     while (*s && *s == ' ')
  186.     s++;
  187.     /* copy port name */
  188.     d = filename;
  189.     while (*s && *s != ' ')
  190.     *d++ = *s++;
  191.     *d = '\0';
  192.  
  193.     lpfnSpoolProc = (DLGPROC) MakeProcInstance((FARPROC) SpoolDlgProc, phInstance);
  194.     hwndspl = CreateDialog(phInstance, "SpoolDlgBox", HWND_DESKTOP, lpfnSpoolProc);
  195.  
  196.     return;
  197. }
  198.  
  199. int PASCAL
  200. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int cmdShow)
  201. {
  202.     MSG msg;
  203.  
  204.     phInstance = hInstance;
  205.  
  206.     init_window(lpszCmdLine);
  207.     ShowWindow(hwndspl, cmdShow);
  208.  
  209.     if (!spoolfile(port, filename)) {
  210.     /* wait, showing error message */
  211.     SetWindowText(GetDlgItem(hwndspl, ID_TEXT), error_message);
  212.     while (GetMessage(&msg, (HWND) NULL, 0, 0)) {
  213.         TranslateMessage(&msg);
  214.         DispatchMessage(&msg);
  215.     }
  216.     }
  217.     DestroyWindow(hwndspl);
  218.     FreeProcInstance((FARPROC) lpfnSpoolProc);
  219.     return 0;
  220. }
  221.